import 'package:BeeApp/MainPage/MainModelPage.dart';
import 'package:BeeApp/MainPage/MainOrderModel.dart';
import 'package:BeeApp/MainPage/MainToolPage.dart';
import 'package:BeeApp/MainPage/http_request.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';

class MainOrderEndNewsPage extends StatefulWidget {
  final String orderID;
  final bool isSend;
  MainOrderEndNewsPage(this.orderID, this.isSend, {Key key}) : super(key: key);

  @override
  _MainOrderEndNewsPageState createState() => _MainOrderEndNewsPageState();
}

class _MainOrderEndNewsPageState extends State<MainOrderEndNewsPage> {
  MainOrderNewsModel newsModel;
  final otherText = TextEditingController();
  double showViewH;
  int showChooseType = 0;

  @override
  void initState() {
    super.initState();
    uploadOrderData();
  }

  void uploadOrderData() {
    DioUtils.postHttp(
      context,
      "Usercommon/order_detail",
      parameters: {
        "token": getData("token"),
        "order_id": widget.orderID,
      },
      onSuccess: (t) {
        print(t);
        MainModel model = MainModel.fromJson(t);
        if (model.code == 1 && model.data != null) {
          if (mounted) {
            setState(() {
              newsModel = MainOrderNewsModel.fromJson(t);
              print("订单类型" + newsModel.data.orderStatus.toString());
            });
          }
        } else {
          showToast(model.msg);
        }
      },
      onError: (error) {
        showToast(error);
        print(error);
      },
    );
  }

  Text labelText01(String str) {
    return Text(
      str,
      style: TextStyle(
        color: Color(0xFF333333),
        fontSize: setFontSize(30),
        fontWeight: FontWeight.bold,
      ),
    );
  }

  Text labelText02(String str) {
    return Text(
      str,
      style: TextStyle(
        color: Color(0xFF333333),
        fontSize: setFontSize(28),
      ),
    );
  }

  Widget mainLineView() {
    return Container(
      width: 900,
      height: 8,
      color: Color(0xFFF5F5F5),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: getAppBar(context, "订单详情"),
      body: newsModel == null || newsModel.code != 1
          ? Container()
          : ListView(
              children: <Widget>[
                mainLineView(),
                address01(true),
                lineView(),
                address02(false),
                mainLineView(),
                newsView(),
                SizedBox(height: 30),
                widget.isSend && newsModel.data.orderStatus == 4
                    ? btnView()
                    : Container(),
              ],
            ),
    );
  }

  Widget address01(bool isSend) {
    return Container(
      color: Colors.white,
      child: Row(
        children: <Widget>[
          leftView(isSend),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                labelText02(newsModel.data.sTitle),
                labelText02(
                  newsModel.data.sAddress + newsModel.data.sBuildingNumber,
                ),
                labelText02(
                    "${newsModel.data.sRealname}  ${newsModel.data.sMobile}"),
              ],
            ),
          ),
          SizedBox(width: setWidth(40)),
        ],
      ),
    );
  }

  Widget lineView() {
    return Container(
      color: Colors.white,
      child: Row(
        children: <Widget>[
          Container(
            padding: EdgeInsets.fromLTRB(
              setWidth(50),
              setHeight(0),
              setWidth(20),
              setHeight(0),
            ),
            child: Container(
              height: setHeight(56),
              width: setHeight(56),
              alignment: Alignment.center,
              child: Image.asset("images/MainChangeVImage.png"),
            ),
          ),
          Expanded(child: Container(height: 1, color: Colors.grey[300])),
          SizedBox(width: setWidth(60)),
        ],
      ),
    );
  }

  Widget address02(bool isSend) {
    return Container(
      color: Colors.white,
      child: Row(
        children: <Widget>[
          leftView(isSend),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                labelText02(newsModel.data.rTitle),
                labelText02(
                    newsModel.data.rAddress + newsModel.data.rBuildingNumber),
                labelText02(
                    "${newsModel.data.rRealname}  ${newsModel.data.rMobile}"),
              ],
            ),
          ),
          SizedBox(width: setWidth(40)),
        ],
      ),
    );
  }

  Widget newsView() {
    return Container(
      color: Colors.white,
      padding: EdgeInsets.all(20),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Text(
            "订单信息",
            style: TextStyle(
              color: Color(0xFF333333),
              fontSize: setFontSize(32),
              fontWeight: FontWeight.bold,
            ),
          ),
          SizedBox(height: 8),
          rowView("订单编号: ", newsModel.data.orderNumber),
          rowView("接单时间: ", newsModel.data.reciveOrderTime),
          rowView("取件时间: ", newsModel.data.pickUpTime),
          rowView("送达时间: ", newsModel.data.sendToTime),
          newsStr().length != 0 ? rowView("货物信息: ", newsStr()) : Container(),
          rowView("备       注: ", newsModel.data.goodsRemark),
          newsModel.data.orderStatus == 7
              ? rowView("评        价: ", newsModel.data.comment)
              : Container()
          // personView(),
        ],
      ),
    );
  }

  // ignore: missing_return
  String newsStr() {
    String shopnews = "";
    if (newsModel.data.csGoodsNumber != 0) {
      shopnews = shopnews +
          newsModel.data.csGoodsName +
          newsModel.data.csGoodsNumber.toString();
    }
    if (newsModel.data.bsGoodsNumber != 0) {
      shopnews = shopnews +
          " " +
          newsModel.data.bsGoodsName +
          newsModel.data.bsGoodsNumber.toString();
    }

    if (newsModel.data.otherGoodsName != null) {
      shopnews = shopnews + " " + newsModel.data.otherGoodsName;
    }

    return shopnews;
  }

  Widget rowView(String typeStr, String content) {
    return Column(
      children: <Widget>[
        Row(
          children: <Widget>[
            Text(
              typeStr,
              style: TextStyle(
                color: Color(0xFF333333),
                fontSize: setFontSize(30),
              ),
            ),
            Expanded(
              child: Text(
                content,
                style: TextStyle(
                  color: Color(0xFF333333),
                  fontSize: setFontSize(28),
                ),
              ),
            )
          ],
        ),
        SizedBox(height: setHeight(10)),
      ],
    );
  }

  Widget timeView01() {
    return Container(
      padding: EdgeInsets.only(bottom: 8),
      // child: labelText("接单时间: 2020-09-10 10:00:00"),
      child: Row(children: <Widget>[
        labelText01("接单时间:"),
        labelText02("2020-09-10 10:00:00")
      ]),
    );
  }

  Widget timeView02() {
    return Container(
      padding: EdgeInsets.only(bottom: 8),
      // child: labelText("送达时间: 2020-09-10 10:00:00"),
      child: Row(children: <Widget>[
        labelText01("送达时间:"),
        labelText02("2020-09-10 10:00:00")
      ]),
    );
  }

  Widget personView() {
    return Container(
      padding: EdgeInsets.only(bottom: 8),
      child: Row(
        children: <Widget>[
          labelText01("收  货  人: "),
          Text(
            "已付款",
            style: TextStyle(
              color: Colors.orange,
              fontSize: setFontSize(30),
            ),
          )
        ],
      ),
    );
  }

  Widget btnView() {
    return Container(
      padding: EdgeInsets.all(setWidth(20)),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          Text(
            "共计:${newsModel.data.orderPrice}元",
            style: TextStyle(
              color: Colors.red,
              fontSize: setFontSize(34),
            ),
          ),
          InkWell(
            child: Container(
              decoration: BoxDecoration(
                  color: Color(0xFFFF7F27),
                  borderRadius: BorderRadius.all(Radius.circular(6)),
                  border: Border.all(color: Color(0xFF333333), width: 2)),
              child: Container(
                margin: EdgeInsets.fromLTRB(20, 8, 20, 8),
                child: Text(
                  "去评价此单",
                  style: TextStyle(
                    color: Color(0xFF333333),
                    fontSize: setFontSize(30),
                    fontWeight: FontWeight.w700,
                  ),
                ),
              ),
            ),
            onTap: () {
              print("object");
              showViewH = 450;
              showView(context);
            },
          ),
        ],
      ),
    );
  }

  void showView(BuildContext context) {
    showModalBottomSheet(
      context: context,
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
      enableDrag: false, //设置不能拖拽关闭
      isDismissible: false, //设置不能点击消失
      isScrollControlled: true,
      builder: (BuildContext context) {
        return StatefulBuilder(
          builder: (BuildContext context, setBottomSheetState) {
            return InkWell(
              child: Container(
                height: showViewH,
                // padding: EdgeInsets.all(10),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.only(
                      topLeft: Radius.circular(15),
                      topRight: Radius.circular(15)),
                ),
                child: ListView(
                  children: <Widget>[
                    Row(
                      mainAxisAlignment: MainAxisAlignment.end,
                      children: [
                        IconButton(
                          icon: Icon(Icons.clear),
                          onPressed: () {
                            Navigator.of(context).pop();
                          },
                        ),
                      ],
                    ),
                    carUserNews(),
                    SizedBox(height: setHeight(60)),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: <Widget>[
                        InkWell(
                          child: showChooseView("好评", 1),
                          onTap: () {
                            setBottomSheetState(() {
                              showChooseType = 1;
                              print(showChooseType);
                            });
                          },
                        ),
                        InkWell(
                          child: showChooseView("一般", 2),
                          onTap: () {
                            setBottomSheetState(() {
                              showChooseType = 2;
                              print(showChooseType);
                            });
                          },
                        ),
                        InkWell(
                          child: showChooseView("差评", 3),
                          onTap: () {
                            setBottomSheetState(() {
                              showChooseType = 3;
                              print(showChooseType);
                            });
                          },
                        ),
                      ],
                    ),
                    SizedBox(height: setHeight(40)),
                    Row(
                      children: <Widget>[
                        SizedBox(width: setWidth(60)),
                        Text(
                          "其他",
                          style: TextStyle(
                            color: Color(0xFF666666),
                            fontSize: setFontSize(30),
                          ),
                        ),
                      ],
                    ),
                    SizedBox(height: setHeight(10)),
                    Container(
                      padding: EdgeInsets.only(
                          left: setWidth(60), right: setWidth(60)),
                      child: Container(
                        decoration: BoxDecoration(
                          // borderRadius: BorderRadius.all(Radius.circular(10)),
                          border: Border.all(
                              color: Color(0xFF707070), width: 1), //边框
                        ),
                        child: TextField(
                          maxLines: 5,
                          controller: otherText,
                          textAlign: TextAlign.start,
                          cursorColor: Colors.grey[400], //设置光标
                          decoration: InputDecoration(
                            border: InputBorder.none,
                            hintText: "其他",
                            hintStyle: TextStyle(
                              fontSize: setFontSize(30),
                              color: Color(0xFF999999),
                            ),
                          ),
                          style: TextStyle(
                            fontSize: setFontSize(30),
                            color: Color(0xFF999999),
                          ),
                          onTap: () {
                            setBottomSheetState(() {
                              showViewH = screenHeight;
                            });
                          },
                        ),
                      ),
                    ),
                    SizedBox(height: setHeight(40)),
                    plBtnView(),
                  ],
                ),
              ),
              onTap: () {
                setBottomSheetState(() {
                  FocusScope.of(context).requestFocus(FocusNode());
                  showViewH = 450;
                });
              },
            );
          },
        );
      },
    ).then((val) {
      print(val);
    });
  }

  Text showText(String string) {
    return Text(
      string,
      style: TextStyle(
        color: Color(0xFF333333),
        fontSize: setFontSize(34),
      ),
    );
  }

  Widget carUserNews() {
    return Row(children: <Widget>[
      SizedBox(width: setWidth(30)),
      ClipOval(
        child: CachedNetworkImage(
          imageUrl: newsModel.data.avatar,
          width: setWidth(110),
          height: setWidth(110),
          fit: BoxFit.cover,
        ),
      ),
      SizedBox(width: setWidth(20)),
      Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          showText(newsModel.data.realname + "  " + newsModel.data.carNumber),
          showText(newsModel.data.mobile),
        ],
      ),
    ]);
  }

  Widget showChooseView(String string, int index) {
    return Container(
      padding: EdgeInsets.fromLTRB(
        setWidth(60),
        setHeight(20),
        setWidth(60),
        setHeight(20),
      ),
      decoration: BoxDecoration(
          color: showChooseType == index ? Color(0xFFFF8629) : Colors.white,
          borderRadius: BorderRadius.all(Radius.circular(2)),
          border: Border.all(
              color: showChooseType == index
                  ? Color(0xFFFF8629)
                  : Color(0xFFC5C5C5),
              width: 1)),
      child: Text(
        string,
        style: TextStyle(
          color: showChooseType == index ? Colors.white : Color(0xFF666666),
          fontSize: setFontSize(30),
        ),
      ),
    );
  }

  Widget plBtnView() {
    return Container(
        alignment: Alignment.center,
        child: InkWell(
          child: Container(
            height: setHeight(88),
            width: screenWidth - setWidth(140),
            alignment: Alignment.center,
            decoration: BoxDecoration(
              color: Color(0xFFFF8629),
              borderRadius: BorderRadius.all(Radius.circular(10)),
              border: Border.all(color: Color(0xFF333333), width: 2), //边框
            ),
            child: Text(
              "发表评价",
              style: TextStyle(
                color: Color(0xFF222222),
                fontSize: setFontSize(32),
              ),
            ),
          ),
          onTap: () {
            print("发表评价" + otherText.text.toString());
            uploadPL();
          },
        ));
  }

  void uploadPL() {
    if (otherText.text.length == 0) {
      showToast("请输入评论内容");
      return;
    }
    showLoading(context);
    DioUtils.postHttp(
      context,
      "goodsuser/save_comment",
      parameters: {
        "token": getData("token"),
        "order_id": widget.orderID,
        "comment_type": showChooseType.toString(),
        "content": otherText.text
      },
      onSuccess: (t) {
        print(t);
        dismissLoading(context);
        showToast(t["msg"].toString());
        if (t["code"].toString() == "1") {
          Navigator.pop(context);
          uploadOrderData();
        }
      },
      onError: (error) {
        dismissLoading(context);
        print(error);
      },
    );
  }
}
